package xworker.swt.dataObject.datastore;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.util.UtilString;
import xworker.dataObject.DataObject;
import xworker.dataObject.DataStore;
import xworker.dataObject.DataStoreListener;
import xworker.lang.executor.Executor;
import xworker.swt.form.SetableModifyListener;

import java.util.List;
import java.util.Map;

public class CheckBoxDataStoreListener extends ControlDataStoreListener implements DataStoreListener, Listener {
    private static final String TAG = CheckBoxDataStoreListener.class.getName();

    Composite composite;
    SetableModifyListener modifyListener;
    boolean radio;

    public CheckBoxDataStoreListener(Composite composite, DataStore dataStore, boolean radio, ActionContext actionContext){
        super(composite, dataStore, actionContext);

        this.composite = composite;
        this.radio = radio;

        onReconfig(dataStore, dataStore.getDataObject());
        if(dataStore.isLoaded()){
            onLoaded(dataStore);
        }
    }

    public boolean isRadio() {
        return radio;
    }

    public SetableModifyListener getModifyListener() {
        return modifyListener;
    }

    public void setModifyListener(SetableModifyListener modifyListener) {
        this.modifyListener = modifyListener;
    }

    public Composite getComposite() {
        return composite;
    }

    @Override
    public void onReconfig(DataStore dataStore, Thing dataObject) {
        exec(() ->{
            try{
                //删除所有数据
                this.clearSelection();
                for(Control children : composite.getChildren()){
                    children.dispose();
                }

                //重新组建表格的列
                if(dataObject == null){
                    return;
                }

                composite.layout();
                composite.getParent().layout();
            }catch(Throwable t){
                Executor.error(TAG, "onReconfig error", t);
            }
        });
    }

    @Override
    public void onLoaded(DataStore dataStore) {
        exec(() ->{
            try{
                //删除所有数据
                for(Control children : composite.getChildren()){
                    children.dispose();
                }

                List<DataObject> records = dataStore.getDatas();
                this.setAllItems(records);
                if(records != null){
                    boolean lisEnable = false;
                    if(modifyListener != null){
                        lisEnable = modifyListener.isEnable();
                        modifyListener.setEnable(false);
                    }
                    try{
                        for(DataObject record : records){
                            //button
                            Button button = new Button(composite, isRadio() ? SWT.RADIO : SWT.CHECK);
                            button.setText(record.getLabel());
                            button.setData(record);
                            button.addListener(SWT.Selection, this);
                        }
                    }finally{
                        if(modifyListener != null){
                            modifyListener.setEnable(lisEnable);
                        }
                    }

                    this.selectionChanged();

                    composite.layout();
                    composite.getParent().layout();
                }
            }catch(Throwable t){
                Executor.error(TAG, "onLoaded error", t);
            }
        });
    }

    @Override
    public void onTotalCountUpdated(DataStore dataStore) {

    }

    @Override
    public void onChanged(DataStore dataStore) {
        onLoaded(dataStore);

        this.selectionChanged();
    }

    @Override
    public void beforeLoad(DataStore dataStore, Thing condition, Map<String, Object> params) {
        exec(() ->{
            for(Control children : composite.getChildren()){
                children.dispose();
            }

            Label label = new Label(composite, SWT.NONE);
            label.setText(UtilString.getString("lang:d=正在加载...&en=Loading...", actionContext));
        });
    }

    @Override
    public void handleEvent(Event event) {
        Button button = (Button) event.item;
        DataObject dataObject = (DataObject) event.item.getData();

        if(button.getSelection()){
            this.addSelection(dataObject);
        }else{
            this.removeSelection(dataObject);
        }
    }

    @Override
    protected void selectionChanged() {
        exec(() ->{
            for(Control control : composite.getChildren()){
                if(control instanceof Button){
                    Button button = (Button) control;
                    DataObject data = (DataObject) button.getData();

                    boolean ok = false;
                    for(DataObject item : getSelectedItems()){
                        if(data.equals(item)){
                            ok = true;
                            break;
                        }
                    }

                    button.setSelection(ok);
                }
            }
        });
    }
}